home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LTP_ConvertNum.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  1KB  |  102 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #ifdef DO_HEXHOOK
  15.  
  16.     // Check whether the buffer only contains a valid hex/binary/octal number...
  17.  
  18. BOOL
  19. LTP_ConvertNum(BOOL negAllowed,STRPTR buffer,LONG *value)
  20. {
  21.     ULONG    num;
  22.     LONG    neg;
  23.     LONG    ch;
  24.  
  25.     num = 0;
  26.  
  27.     if((buffer[0] == '-') && negAllowed)
  28.     {
  29.         neg = -1;
  30.  
  31.         buffer++;
  32.     }
  33.     else
  34.         neg = 1;
  35.  
  36.     if(((buffer[0] == '0') && ToUpper(buffer[1] == 'X')) || (buffer[0] == '$'))
  37.     {
  38.         if(*buffer++ != '$')
  39.             buffer++;
  40.  
  41.         while(ch = ToUpper(*buffer++))
  42.         {
  43.             num = num * 16;
  44.  
  45.             if((ch >= 'A') && (ch <= 'F'))
  46.                 num += (ch - 'A') + 10;
  47.             else
  48.             {
  49.                 if ((ch >= '0') && (ch <= '9'))
  50.                     num += ch - '0';
  51.                 else
  52.                     return(FALSE);
  53.             }
  54.         }
  55.     }
  56.     else
  57.     {
  58.         if(*buffer == '%')
  59.         {
  60.             buffer++;
  61.  
  62.             while(ch = *buffer++)
  63.             {
  64.                 if((ch < '0') || (ch > '1'))
  65.                     return(FALSE);
  66.                 else
  67.                     num = (num * 2) + (ch - '0');
  68.             }
  69.         }
  70.         else
  71.         {
  72.             if(*buffer == '&')
  73.             {
  74.                 buffer++;
  75.  
  76.                 while(ch = *buffer++)
  77.                 {
  78.                     if((ch < '0') || (ch > '7'))
  79.                         return(FALSE);
  80.                     else
  81.                         num = (num * 8) + (ch - '0');
  82.                 }
  83.             }
  84.             else
  85.             {
  86.                 while(ch = *buffer++)
  87.                 {
  88.                     if((ch < '0') || (ch > '9'))
  89.                         return(FALSE);
  90.                     else
  91.                         num = (num * 10) + (ch - '0');
  92.                 }
  93.             }
  94.         }
  95.     }
  96.  
  97.     *value = num * neg;
  98.  
  99.     return(TRUE);
  100. }
  101. #endif
  102.